local ug_player = {}
ug_player.skills = {}
ug_player.stats = {}
ug_player.ListOfCorpses = {}
ug_player.ListOfCorpses["n"] = 0
ug_player.stats["str"] = 100
ug_player.stats["dex"] = 100
ug_player.stats["int"] = 100
ug_player.stats["hits"] = 100
ug_player.stats["stam"] = 100
ug_player.stats["mana"] = 100
ug_player.IsDead = false
ug_player.player = {}

function ug_player:DropCorpse()
	corpse = ents.Create("prop_physics")
	corpse:SetModel(self.player:GetModel())
	corpse:SetPos(self.player:GetPos())
	self.ListOfCorpses["n"] = self.ListOfCorpses["n"] + 1
	self.ListOfCorpses[self.ListOfCorpses["n"]] = corpse
end

function ug_player:Resurrect()
	self:Ghost(false)
	self.IsDead = false
	umsg.Start("renderdeath",ply)
		umsg.Bool(false)
	umsg.End()
	self.player:SetHealth(math.Round(self.stats["str"]*20/100))
	local nearest = nil
	for v,k in pairs(self.ListOfCorpses) do
		if v != "n" then
			if nearest == nil then
				nearest = k
			end
			if k:GetPos():Distance(self.player:GetPos()) < nearest:GetPos():Distance(self.player:GetPos()) then
				nearest = k
			end
		end
	end
	if nearest:GetPos():Distance(self.player:GetPos()) < 500 then
		self.player:SetPos(nearest:GetPos())
	end
end

function ug_player:ShowInHead(text)
end

function ug_player:Ghost(bool)
	if bool then
		self.player:SetMoveType(MOVETYPE_FLY)
	else
		self.player:SetMoveType(MOVETYPE_WALK)
	end
end

function ug_player:playerDies( weapon, killer )
	victim = self.player
	--victim:SetHealth(1)
	victim.ug.IsDead = true
	umsg.Start("renderdeath",ply)
		umsg.Bool(true)
	umsg.End()
	self:DropCorpse()
	victim.ug:Ghost(true)
end

-- Ended ug_player table

function GM:CanPlayerSuicide ( ply )
	ply:PrintMessage(HUD_PRINTTALK, "You suicided")
	ply.ug:playerDies(nil, nil)
	ply:SetHealth(1)
	return false
end 

function GM:EntityTakeDamage( ent, inflictor, attacker, amount ) 
	if !ent:IsPlayer() then
		return true
	elseif ent.ug.IsDead then
		ent:SetHealth(ent:Health() + amount)
	elseif inflictor == ent.Holding then
		ent:SetHealth(ent:Health() + amount)
		return false
	elseif amount >= ent:Health() then
		ent:SetHealth(amount + 1)
		ent.ug:playerDies( inflictor, attacker)
	end
end

function ISaid( ply, text, toall )
	if toall then
		if ply.ug.IsDead then
			text = string.sub("ooOoOoOOoOOooooOOoooOoOoOoOOoOoooOOOoOOo",math.random(20),math.random(21,40))
		end
	end
	//ply.ug:ShowInHead(text) this will be when done.
	return text
end
hook.Add( "PlayerSay", "ISaid", ISaid ); --Function when you say something

function GM:PlayerInitialSpawn( ply )
	ply.ug = ug_player
	ply.ug.player = ply
end

function GM:PlayerSpawn( ply )

end

function GM:PlayerNoClip( ply )
	if ply:IsAdmin() then 
		return true 
	else
		return false
	end
end